﻿using System;
using System.Collections.Specialized;
using System.Runtime.Serialization;

namespace SimpleServiceBus.Bus
{
    [Serializable]
    public class Headers : NameValueCollection
    {
        protected Headers(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }

        public Headers()
        {
        }

        public T GetValue<T>(string headerKey)
        {
            string val = this[headerKey];
            if (val != null)
            {
                if (typeof(T) == typeof(TimeSpan))
                    return (T) (object) TimeSpan.Parse(val);

                return (T) Convert.ChangeType(val, typeof (T));
            }
            return default(T);
        }
    }
}
